package com.itfreer.power.service.jurisdiction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.imp.DictionaryServiceImp;
import com.itfreer.form.dictionary.base.DictionaryItem;
import com.itfreer.power.dao.jurisdiction.MapLayerJurisdictionDao;
import com.itfreer.power.entity.jurisdiction.MapLayerJurisdictionEntity;
import com.itfreer.power.entity.jurisdiction.MapLayerJurisdictionItemEntity;

@Component("mapLayerJurisdictionService")
public class MapLayerJurisdictionServiceBase extends DictionaryServiceImp<MapLayerJurisdictionEntity> implements MapLayerJurisdictionService {

	@Autowired
	private MapLayerJurisdictionDao mapLayerJurisdictionDao;

	@Override
	protected BaseDao<MapLayerJurisdictionEntity> getDao() {
		return mapLayerJurisdictionDao;
	}

	@Override
	public String getDictionaryName() {
		return "p_maplayer_jurisdiction";
	}

	@Override
	protected DictionaryItem convert(MapLayerJurisdictionEntity entity) {
		DictionaryItem toEntity = new DictionaryItem();
		BeanUtils.copyProperties(entity, toEntity);
		Set<MapLayerJurisdictionItemEntity> items = entity.getItems();
		if (items != null && items.size() > 0) {
			Map<String, List<String>> itemCache = new HashMap<String, List<String>>();
			for (MapLayerJurisdictionItemEntity item : items) {
				String key = item.getLayerUrl().toLowerCase();
				if (itemCache.containsKey(key)) {
					itemCache.get(key).add(item.getLayerWhere());
				} else {
					List<String> tables = new ArrayList<String>();
					tables.add(item.getLayerWhere());
					itemCache.put(key, tables);
				}
			}
			toEntity.getOther().put("items", itemCache);
		}
		toEntity.getOther().put("registerService", entity.getRegisterService());
		return toEntity;
	}

	@Override
	protected String getDictionaryFields() {
		return null;
	}
}
